// 用户管理数据
import { defineStore } from 'pinia'
import { getUserList } from '@/apis/system/userInfo'
import { ElMessage } from 'element-plus'
import { Itype } from '@/type'

interface IuserList {
  id: number
  name: string
  avatar: string
}
const userlistData: IuserList[] = []

export const userStore = defineStore('userData', {
  state: () => {
    return {
      userRoles: [''], // 用户权限
      userlistData // 所有用户
    }
  },
  getters: {},
  actions: {
    // 获取所有用户信息
    async getUserData() {
      await getUserList()
        .then((res) => {
          if (!res) return
            let arr: any = []
            arr = res
            arr.forEach((element: Itype) => {
              element.created_at = new Date(
                +new Date(new Date(element.created_at).toJSON()) + 8 * 3600 * 1000
              )
                .toISOString()
                .replace(/T/g, ' ')
                .replace(/\.[\d]{3}Z/, '')
            })
            this.$state.userlistData = arr
        })
        .catch(() => {
          ElMessage('服务器内部错误')
        })
    }
  },
  persist: true // 本地持久化
})
